package org.zcode.generator.core.generator.clazz;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.zcode.generator.core.bean.CreateApiServiceIPo;
import org.zcode.generator.core.util.HttpUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @author zhoulk
 * @date: 2019/10/15
 */
public class DesignSystemCreator {

    static String host = "http://10.1.236.14";
    static String addServiceApiDesUrl = "/sysMgr/baseapi/serviceApiDes/serviceApiDes";
    static String qryMaxCodeUrl = "/sysMgr/baseapi/serviceApiDes/combineCode";
    static Map<String, String> headers = new HashMap<>();

    static {
        headers.put("Content-Type", "application/json;charset=UTF-8");
    }

    public static void createApi(CreateApiServiceIPo inParam) {
        Map<String, String> querys = new HashMap<>();
        Map<String, Object> result;
        String maxCode;
        try {

            maxCode = qryMaxCode(inParam.getApiType(), "I" + inParam.getServiceCode());
        } catch (Exception e) {
            System.out.println("【错误提醒】添加设计系统接口失败；【接口名】" + inParam.getApiName());
            return;
        }
        inParam.setApiCode("I" + inParam.getServiceCode() + maxCode);
        try {
            HttpResponse response = HttpUtils.doPost(host, addServiceApiDesUrl, headers, querys, JSON.toJSONString(inParam));
            result = JSON.parseObject(EntityUtils.toString(response.getEntity()), new TypeReference<Map<String, Object>>() {
            });
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取当前最大编码
     * @param codeType
     * @param sysCode
     * @return
     */
    private static String qryMaxCode(Integer codeType, String sysCode) throws Exception {
        Map<String, String> querys = new HashMap<>();
        Map<String, Map<String, String>> result;
        querys.put("codeType", codeType + "");
        querys.put("sysCode", sysCode);
        HttpResponse response = HttpUtils.doGet(host, qryMaxCodeUrl, headers, querys);
        result = JSON.parseObject(EntityUtils.toString(response.getEntity()), new TypeReference<Map<String, Map<String, String>>>() {
        });
        return result.get("data").get("maxCode");
    }

    public static void main(String[] args) {
        Map<String, String> headers = new HashMap<>();
        Map<String, String> querys = new HashMap<>();
        Map<String, String> bodys = new HashMap<>();
        Map<String, Object> result = new HashMap<>();
        try {
//            querys.put("codeType", "1");
//            querys.put("isIntroTmpl", "0");
            bodys.put("apiCode", "I2011028");
            bodys.put("apiName", "'test3'");
            bodys.put("apiType", "2");
            bodys.put("createOpname", "zhoulk");
            bodys.put("serviceCode", "201");
            bodys.put("taskCode", "R100009");
//            headers.put("Cookie", "JSESSIONID=3c484881910a4a67b1bda4b83a7dcc0f; username=zhoulk;");
            headers.put("Content-Type", "application/json;charset=UTF-8");
            HttpResponse response = HttpUtils.doPost(host, addServiceApiDesUrl, headers, querys, JSON.toJSONString(bodys));
            result = JSON.parseObject(EntityUtils.toString(response.getEntity()), Map.class);
            System.out.println(result);
        } catch (Exception e) {


        }
    }

}
